//
// Author    : Pablo Bosch 
// Contact   : pboschvivancos@epo.org
// Created   : 08/05/2005
// Last Modif: 09/05/2005
//

var MOD_EDGRP={};

MOD_EDGRP.askdelgrp=function(idx) {
  var grname=GetGroupName(idx);
  if(confirm("Delete group "+grname+" ?")) {    
    var cl_iter = new claim_iterator();
    cl_iter.map(function(cl) { if(GetClaimGroup(cl)==grname) SetClaimGroup(cl,"");});
    DeleteGroup(idx);   
    edit_groups();
  }
}

MOD_EDGRP.addgrp=function(title) {
  AddGroup();
  //UpdateGroups();
  edit_groups();
}
  
MOD_EDGRP.onchange_grpclms=function(idx,clist) {
  var cl_iter = new claim_iterator();
  var grname=GetGroupName(idx);

  if(!clist) {
      clist=html_fixarea_selected().value;
  }
  // first delete all claims in group grname
  while(!cl_iter.end()) {
    if(cl_iter.getGroup()==grname) {
      cl_iter.setGroup("");
    }
    cl_iter.next();
  }
  // now add claims in group grname 
  var cl_iter = new List_iterator(list_toList(clist));
  while(!cl_iter.end()) {
    SetClaimGroup(cl_iter.getValue(),grname);
    //alert(cl_iter.getValue()+" "+cl_iter.end()); 
    cl_iter.next();
  }
  
  // Update special technical feature claim
  grp_update_stfcl(GetGroupsDoc(),grname);

  edit_groups();
} 


MOD_EDGRP.onchangegrpdoc=function() {
  MOD_EDGRP.grpseldoc=html_selected("grpseldoc",ALLDOCPNG);
  SetGroupsDoc(MOD_EDGRP.grpseldoc);
  edit_groups();
}


function select_nogroup() {
  var cl_iter = new claim_iterator();
  return cl_iter.select(function(cl) { return !GetClaimGroup(cl);}).compact();
}     


function select_warningsgroup() {
  var cl_iter = new claim_iterator();
  return cl_iter.select(function(cl) { return GetClaimGroupWarn(cl);}).compact();
}

function select_warningsgrouptxt(){
  var cl_iter = new claim_iterator();
  return cl_iter.reduce(function(cl,txt) { var w=GetClaimGroupWarn(cl); return  w ? ((txt||"")+cl+":"+w+"\n"):txt});
}


MOD_EDGRP.onchange_grpstf=function(grpseldoc,g) {
  SetGroupSTF(grpseldoc,g,html_flexarea_selected().value);		      		
}

MOD_EDGRP.onchage_grppblm=function(grpseldoc,g) {
  SetGroupProblem(grpseldoc,g,html_flexarea_selected().value);				
}

MOD_EDGRP.switchpaid=function(g) {
  if(IsGroupPaid(g)) {
    SetGroupNotPaid(g);
  } else {
    SetGroupPaid(g);
  }
}

MOD_EDGRP.onchangeprefcl=function(seldoc,g,cl) {
  SetGroupPrefClaim(seldoc,g,cl);
  edit_groups();
}

MOD_EDGRP.onchgrouptitle=function(group) {
  SetGroupTitle(GetGroupsDoc(),group,html_fixarea_selected().value);
  //alert("onchgrouptitle("+group+","+GetGroupTitle(GetGroupsDoc(),group)+")");
}

MOD_EDGRP.onexportgroups=function() {
  if(confirm("Export groups to Caesar: Check that Caesar is not running.\nThis will modify your search report. Proceed ?")) {    
    CaesarExportNU(GetDossierNumber());
  }
}
MOD_EDGRP.onswapgroups=function(g) {
  SwapGroupsDown(g);
  edit_groups();
}
    
// find special technical features and corresponding claim  in group g using doc grpseldoc
MOD_EDGRP.grpseldoc;

function edit_groups() {  
  InitDocPNG();
  MOD_EDGRP.grpseldoc=GetGroupsDoc();
  var grpwarn=select_warningsgroup();

  var html="";

  html+="<TABLE border=1>";
  html+="<TR><TH>Group</TH>";
  html+="<TH align=left>Prior art "+html_selection("grpseldoc",ALLDOCPNG,MOD_EDGRP.grpseldoc,"MOD_EDGRP.onchangegrpdoc()")+"</TH>";
  html+="<TH align=left>Ungrouped <input type=text name=nogroup readonly size=45 value="+select_nogroup()+"></TH>";
  if(grpwarn) {
    html+="<TH align=right>"+html_warning("allgrpwarn",select_warningsgrouptxt(),"");
    html+=" <input type=text name=warngroup readonly size=10 value='"+grpwarn+"'></TH>";
  } else {
    html+="<TH></TH>";
  }
  html+="<TH><button name='exp' onClick='MOD_EDGRP.onexportgroups()'>"+html_img(export_img)+"</button></TH></TR>";
  var gr_iter=  new group_iterator();
  while(!gr_iter.end()) {
    var g=gr_iter.getValue();
    html+="<TR><TD align=center class='GRPNAME'>"+gr_iter.getName()+"</TD>";
    html+="<TD colspan=3><TABLE>";
    html+="<TR></TR>";    
    html+="<TR><TD><b>Title</b><BR>";     
    html+=html_fixarea(GetGroupTitle(MOD_EDGRP.grpseldoc,g), 
		       "MOD_EDGRP.onchgrouptitle("+g+");",			
		       "class=RWTXT",40,1)+"</TD>";
    html+="<TD><b>Claims</b><BR>";  
    html+=html_inputtext(GetGroupClaims(gr_iter.getName()), 
			 "MOD_EDGRP.onchange_grpclms("+g+",this.value)",
			 36,
			 "class='RWTXT' id='grplist"+gr_iter.getName()+"'");
    /*
    html+=html_fixarea(GetGroupClaims(gr_iter.getName()), 
		       "MOD_EDGRP.onchange_grpclms("+g+");",			
		       "class='RWTXT' id='grplist"+gr_iter.getName()+"'",20,1)+"</TD>";
    */
    html+="</TD></TR><TR><TD colspan=2><b>Special Technical Features</b><BR>";
    html+=html_flexarea(GetGroupSTF(MOD_EDGRP.grpseldoc,g),"MOD_EDGRP.onchange_grpstf(MOD_EDGRP.grpseldoc,"+g+")","class=RWTXT",65);
    html+="<TD></TR>";         
    html+="<TR><TD colspan=2><b>Differences</b> ";
    html+="(Claim <input type=text size=3 value='"+GetGroupPrefClaim(MOD_EDGRP.grpseldoc,g)+"'";
    html+=" onchange='MOD_EDGRP.onchangeprefcl(\""+MOD_EDGRP.grpseldoc+"\","+g+",this.value)'>)<BR>";
    html+=html_flexarea(find_stfs(MOD_EDGRP.grpseldoc,gr_iter.getName(),GetGroupPrefClaim(MOD_EDGRP.grpseldoc,g))
			,""," readonly class=ROTXT ",65)+"<TD></TR>";        
    html+="<TR><TD colspan=2><b>Associated Problem</b><BR>";
    html+=html_flexarea(GetGroupProblem(MOD_EDGRP.grpseldoc,g),
			"MOD_EDGRP.onchage_grppblm(MOD_EDGRP.grpseldoc,"+g+")","class=RWTXT",65);
    html+="</TD></TR>";
    html+="</TABLE></TD>";
    if(g) {
      html+="<TD VALIGN=top height=100%><TABLE height=100%>";
      html+="<TR valign=top><TD><button onClick='MOD_EDGRP.askdelgrp("+g+");' class='FULLW'>"+html_img(trash_img)+"</button><br>";
      if(g<GetGroupsLength()) {
	html+="<button onClick='MOD_EDGRP.onswapgroups("+g+")'>"+html_img(rotate_img)+"</button>";
      }
      html+="</TD></TR><TR valign=bottom><TD>Paid<br>";
      if(g==1) {
	html+="<input type=checkbox checked disabled></TD></TR>";
      } else {
	html+="<input type=checkbox "+(IsGroupPaid(g) ? "checked":"")+" onClick='MOD_EDGRP.switchpaid("+g+");'></TD></TR>";
      }
      html+="</TABLE></TD>";
    }
    html+="</TR>";
    gr_iter.next();
  }
  html+="<TR><TD align=center >";
  html+="<BUTTON TYPE=Button onclick='MOD_EDGRP.addgrp(\"No title\");' ACCESSKEY='N' class='FULL'>"+html_img(add_img)+"</button></TD><TR>";
  html+="</TABLE>";
  
  
  ClaimToolEditSection("Edit Groups",html,"edit_groups()");
}

